package com.lzy.cloud.ticket.train.facade;

import com.lzy.cloud.ticket.train.constant.LMMApi;
import com.lzy.cloud.ticket.train.constant.LMMContant;
import com.lzy.cloud.ticket.train.dtos.TrainCodeDto;
import com.lzy.cloud.ticket.train.request.TrainCodeRequest;
import com.lzy.cloud.ticket.train.response.TrainCodeResponse;
import com.lzy.cloud.ticket.train.request.BaseRequest;
import com.lzy.cloud.ticket.train.servcie.TrainCodeService;
import com.lzy.cloud.ticket.train.util.BasePushUtil;
import com.lzy.framework.core.utils.DozerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class StationFacade {

    @Resource
    private RestTemplate restTemplate;

    @Autowired
    private BasePushUtil basePushUtil;

    @Autowired
    private TrainCodeService trainCodeService;

    public TrainCodeResponse queryStationList() {
        Map<String, Object> map = new HashMap<>();
        map.put("k", "v");
        BaseRequest baseRequest = basePushUtil.push(LMMContant.APP_KEY, LMMContant.ALL_SATATION_METHOD, LMMContant.MESSAGE_FORMT, LMMContant.CALL_BACK_URL);
        TrainCodeRequest trainCodeRequest = DozerUtils.map(baseRequest, TrainCodeRequest.class);
        trainCodeRequest.setData(map);
        TrainCodeResponse trainCodeResponse = restTemplate.postForObject(LMMApi.ALL_STATION_URL, trainCodeRequest, TrainCodeResponse.class);
        return trainCodeResponse;
    }

    /**
     * 清空站点表
     *
     * @return
     */
    public  boolean deleteTrainStationTable() {
        return trainCodeService.deleteTrainStationTable();
    }

    /**
     * 批量新增站点数据
     *
     * @return
     */
    public  boolean batchInsertTrainCode(List<TrainCodeDto> codeList) {
        return trainCodeService.batchInsertTrainCode(codeList);
    }

    /**
     * 根据站点名,查询站点简码
     * @param trainCodeDto
     * @return
     */
    public TrainCodeResponse queryStationCode(TrainCodeDto trainCodeDto) {
        List<TrainCodeDto> trainCodeDtos = trainCodeService.selectByStationName(trainCodeDto);

        TrainCodeResponse trainCodeResponse = new TrainCodeResponse();
        trainCodeResponse.setData(trainCodeDtos);
        return trainCodeResponse;
    }
}
